
function createMap(elId, centerLon, centerLat, zoom)
{
	var map = new GMap2(document.getElementById(elId));		
	
	var geoResultMarker = null;	
	map.setUI(map.getDefaultUI());
	var trafficOptions = {incidents:true};
	trafficInfo = new GTrafficOverlay(trafficOptions);
	map.addOverlay(trafficInfo);	
	map.setCenter(new GLatLng(centerLat, centerLon), zoom);
	var icons = {};
	
	GEvent.addListener(map, "click", function(overlay, latlng) 
	{
		if (latlng && returnObject.onClick)
		{
			returnObject.onClick(latlng.lat(), latlng.lng());
		}
	});	

	function setCenter(lat, lon, zoom)
	{
		map.setCenter(new GLatLng(lat, lon), zoom);
	}
	
	function find(value, onFound, latlng)
	{
		if (geoResultMarker) map.removeOverlay(geoResultMarker);
		var geocoder = new GClientGeocoder();
		if (latlng)
		{
			map.setCenter(new GLatLng(latlng.lat, latlng.lon));
			if (onFound)
			{			
				onFound(latlng.lat, latlng.lon, true);
			}
			return;
		}
		geocoder.getLatLng(value, function (gLatLng) 
		{
			if (gLatLng) 
			{				
				map.setCenter(gLatLng);
				if (onFound)
				{						
					onFound(gLatLng.lat(), gLatLng.lng(), true);
				}
			}
			else 
			{
				if (onFound)
				{				
					onFound(0, 0, false);
				}
			}
		});		
	}
	
	function addMarker(lat, lon, type, isDraggable, title, description)
	{
		
		var info = '<b>' + title + '</b><br>' + description;
		if (!icons[type])
		{
			icons[type] = new GIcon(G_DEFAULT_ICON);
			icons[type].image = "/static/images/markers/" + type + ".png";
			icons[type].shadow = "";
			icons[type].iconSize = new GSize(32, 37);
			icons[type].infoWindowAnchor = new GPoint(16, 16);
			
			/*
			icons[type].iconSize = new GSize(20, 34);
			icons[type].shadowSize = new GSize(37, 34);			
			icons[type].iconAnchor = new GPoint(9, 34);
			*/
		}		
		
		var marker = new GMarker(new GLatLng(lat, lon), {draggable: isDraggable, icon: icons[type]});
		var returnMarker = {marker: marker, 
			onDragEnd : null, 
			lat: function(){return marker.getLatLng().lat();}, 
			lon: function(){return marker.getLatLng().lng();}, 
			showInfo : function(){return marker.openInfoWindow(info);}
		};
		
		GEvent.addListener(marker, "click", function() 
			{										
				this.openInfoWindowHtml(info);
			});		
	
		GEvent.addListener(marker, "dragend", function() 
			{				
				if (returnMarker.onDragEnd) 
				{					
					returnMarker.onDragEnd(); 
				}
			});
					
		
		map.addOverlay(marker);		
		return returnMarker;
	}
	
	function removeMarker(marker)
	{
		if (marker)
			map.removeOverlay(marker.marker);
	}
	
	function openBaloon(lat, lon, text)
	{
		map.openInfoWindow(new GLatLng(lat, lon), text);		
	}
	
	function setBounds(markers)	
	{
		var bounds = new GLatLngBounds(); 		
		for (var i = 0; i < markers.length; i++)
		{
			bounds.extend(markers[i].marker.getLatLng());	
		}
		if (!map.containsBounds(bounds)) 
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));		
	}
	
	function addPolyLine(arrayOfLatLonObjects, lineOptions)
	{
		var geoPoints = new Array();
		
		for (var i = 0; i < arrayOfLatLonObjects.length; i++)
		{
			geoPoints.push(new GLatLng(arrayOfLatLonObjects[i].latitude, arrayOfLatLonObjects[i].longitude ));
		}
		 	
		 
		var polyline = new GPolyline(geoPoints, lineOptions.color, lineOptions.width, lineOptions.opacity);
		map.addOverlay(polyline);
	}
	
	var returnObject = {map : map, find : find, addMarker: addMarker, removeMarker: removeMarker, setCenter: setCenter, onClick : null, openBaloon : openBaloon, setBounds: setBounds, addPolyLine: addPolyLine};
	return returnObject;
}